/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serverComm;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import main.ApplicatieMain;
import view.SpelerOptiesPagina;

/**
 *
 * @author Nick
 */
public class CommConnection implements Runnable {

    private Socket socket;
    private ApplicatieMain applicatieMain;
    
    private PrintWriter out;
    private BufferedReader in;

    private final String SERVERIP = "145.92.6.46";   //Server IP for usage on server
//    private final String SERVERIP = "localhost";   //Localhost IP for local testing
    private final int SERVERPORT = 8069;

    private CommIn commIn;
    private CommOut commOut;
    private boolean serverRunning;
    private int clientId;
    private String clientName;

    public CommConnection() {

    }

    public void run() {
        applicatieMain = ApplicatieMain.getInstance();

        //Initialize connection to server
        serverRunning = connectServer();


        //Set the state of the server
        applicatieMain.setServerRunning(serverRunning);
        System.out.println(applicatieMain.isServerRunning());
    }

    public boolean connectServer() {
        try {
            //Initialize socket connection
            socket = new Socket(SERVERIP, SERVERPORT);

            //Initialize output writer
            out = new PrintWriter(
                    socket.getOutputStream(), true);

            //Initialize input reader
            in = new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));

            //Initialize the input and output Comm's
            System.out.println("LOADING CommOut...");
            commOut = new CommOut(out, this);
            Thread commOutThread = new Thread(commOut);
            commOutThread.start();

            System.out.println("LOADING CommIn...");
            commIn = new CommIn(in, this);
            Thread commInThread = new Thread(commIn);
            commInThread.start();

            System.out.println("DONE");

            //return true if connection has been made
            return true;
        } catch (UnknownHostException e) {
            System.out.println("Unknown host: " + SERVERIP);
            JOptionPane.showMessageDialog(applicatieMain.getContentPane(), "Kon niet verbinden met de KlasseTV server", "ERROR", JOptionPane.ERROR_MESSAGE);
            applicatieMain.showPage(new SpelerOptiesPagina());
            return false;
        } catch (IOException e) {
            System.out.println("No I/O");
            JOptionPane.showMessageDialog(applicatieMain.getContentPane(), "Kon niet verbinden met de KlasseTV server", "ERROR", JOptionPane.ERROR_MESSAGE);
            applicatieMain.showPage(new SpelerOptiesPagina());
            return false;
        }
    }

    public void disconnectServer() {
        try {

            applicatieMain.setServerRunning(false);
            applicatieMain.setCommServer(null);
            System.out.println("UNSET COMMSERVER");
            commOut.closeConnection();
            out.close();
            in.close();
            socket.close();
            applicatieMain.setNotifyLobbyUpdate(true);


        } catch (IOException ex) {
            Logger.getLogger(CommConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getClientId() {
        return clientId;
    }

    public void setClientId(int clientId) {
        this.clientId = clientId;
    }

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    public CommOut getCommOut() {
        return commOut;
    }
}
